package com.iamteer.contact;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class Main {

	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ContactOperatorImpl operator = new ContactOperatorImpl();
		try {
			
			while (true) {
				printMenu();
				String command = br.readLine();
				if ("1".equals(command)) {
					Contact contact = setContact(br);
					operator.addContact(contact);
				} else if ("2".equals(command)) {
					Contact contact = setContact(br);
					operator.updateContact(contact);
				} else if ("3".equals(command)) {
					System.out.println("请输入删除的编号：");
					String id = br.readLine();
					operator.deleteContact(id);
				} else if ("4".equals(command)) {
					List<Contact> list = operator.findAll();
					for (Contact contact : list) {
						System.out.println(contact);
					}
				} else if ("Q".equalsIgnoreCase(command)) {
					break;
				}
			}
			
		}catch (IOException e) {
				e.printStackTrace();
		}
	}

	private static void printMenu() {
		System.out.println("============================");
		System.out.println("[1]增加联系人");
		System.out.println("[2]修改联系人");
		System.out.println("[3]删除联系人");
		System.out.println("[4]查看所有联系人");
		System.out.println("[Q]退出系统");
		System.out.println("============================");
	}
	
	private static Contact setContact(BufferedReader br) {
		try {
			Contact contact = new Contact();
			System.out.println("请输入编号：");
			String id = br.readLine();
			System.out.println("请输入姓名：");
			String name = br.readLine(); 
			System.out.println("请输入性别：");
			String gender = br.readLine(); 
			System.out.println("请输入年龄：");
			String age = br.readLine(); 
			System.out.println("请输入电话：");
			String phone = br.readLine();
			System.out.println("请输入邮箱：");
			String email = br.readLine();
			System.out.println("请输入 QQ：");
			String qq = br.readLine();
			contact.setId(id);
			contact.setName(name);
			contact.setGender(gender);
			contact.setAge(Integer.parseInt(age));
			contact.setPhone(phone);
			contact.setEmail(email);
			contact.setQq(qq);
			return contact;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
